package com.yuyou.fn.tas.pusher.handler;

import com.alibaba.fastjson.JSONArray;
import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.Multimaps;
import com.yuyou.fn.common.database.event.EventHandler;
import com.yuyou.fn.common.database.event.IEventHandler;
import com.yuyou.fn.educational.entity.Teacher;
import com.yuyou.fn.tas.pusher.service.IDataPushService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Set;

@EventHandler(tables = "yyfn.teacher")
@Component
public class TeacherPusher implements IEventHandler {

    @Resource
    private IDataPushService dataPushService;

    @Override
    public void insert(String tableName, JSONArray rowList) {
        handle(rowList);
    }



    @Override
    public void update(String tableName, JSONArray rowList, Set<String> updateColumns) {
        handle(rowList);
    }

    @Override
    public void delete(String tableName, JSONArray rowList) {

    }


    private void handle(JSONArray rowList) {
        List<Teacher> teachers = rowList.toJavaList(Teacher.class);

        //Collection<Teacher> list = Collections2.filter(teachers, (t) ->  StringUtils.isNotEmpty(t.getTeachSubjectIds()) && StringUtils.isNotEmpty(t.getTeachGradeIds()));

        ImmutableListMultimap<Long, Teacher> areaTeamId2Teacher = Multimaps.index(teachers, c -> c.getAreaTeamId());

        for(Long areaTeamId : areaTeamId2Teacher.keySet()){
            dataPushService.pushTeacher(areaTeamId,areaTeamId2Teacher.get(areaTeamId));
        }
    }
}
